import Vue from 'vue'
// export default function yearMonth() {
Vue.component('year-month', {
  props: ['nowdata', 'mindata', 'maxdata'],
  template: yearMonthDom,
  data: function () {
    return {
      dataList: [],
      data: 0,
      min_data: '',
      max_data: '',
      isFirst: true
    }
  },
  created: function () {
    var vm = this;

    var now = new Date(),
      nowYear = now.getFullYear(),
      nowMonth = now.getMonth() + 1;

    vm.min_data = !!vm.mindata ? vm.mindata : '197001';
    vm.max_data = !!vm.maxdata ? vm.maxdata : '209012';

    if (vm.nowdata) {
      if (Number(vm.nowdata) < Number(vm.min_data) || Number(vm.nowdata) > Number(vm.max_data)) {
        alert('当前时间不存在选项中');
        return;
      }
      vm.data = vm.nowdata;
    } else {
      if (Number(nowYear + '' + nowMonth) < vm.min_data) {
        vm.data = vm.min_data;
      } else if (Number(nowYear + '' + nowMonth) > vm.max_data) {
        vm.data = vm.max_data;
      } else {
        vm.data = nowYear + '' + nowMonth;
      }
    }

    if (Number(vm.min_data) > Number(vm.max_data)) {
      alert('最小日期不得大于最大日期');
      return;
    }

    var minYear = Number(vm.min_data.slice(0, 4)), //最小年
      minMonth = Number(vm.min_data.slice(-2)), //最小月
      maxYear = Number(vm.max_data.slice(0, 4)), //最大年
      maxMonth = Number(vm.max_data.slice(-2)), //最大月
      pushYear = minYear; //要加入的年

    while (pushYear <= maxYear) {
      var maxI = pushYear == maxYear ? maxMonth : 12;
      var minI = pushYear == minYear ? minMonth : 1; //要加入的月
      for (var i = 0; i <= maxI - minI; i++) {
        var pushMonth = Number(minI + i) < 10 ? '0' + Number(minI + i) : Number(minI + i) + ''; //月份小于10的，要改成01的形式
        vm.dataList.push({
          data: pushYear + '' + pushMonth,
          year: pushYear,
          month: pushMonth
        });
      }
      pushYear++;
    }

  },
  methods: {
    changeTime: function () {
      var vm = this;
      vm.$emit('changetime', vm.data);
    }
  }
})
// }
